//
//  XRmenuCellBase.m
//  XRmenu
//
//  Created by Chow Akins on 11-11-23.
//  Copyright 2011年 foxhis. All rights reserved.
//

#import "XRmenuCellBase.h"

@implementation XRmenuCellBase
@synthesize currentStyle;
#pragma mark 标准方法及扩展的初始化方法
-(id) initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier Customer:(NSString*)customer
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
		self.currentStyle = customer;
		NSDictionary *dict = [APPSESSION.allconfig findAtPath:[NSString stringWithFormat:@"UIKit CSS/TableView Cell CSS/%@", customer]];
		if (dict) {
			if ([dict objectForKey:@"imageview list"]) {
				[self setImageViewsProperty:[dict objectForKey:@"imageview list"]];
			}
			if ([dict objectForKey:@"label list"]) {
				[self setLabelsProperty:[dict objectForKey:@"label list"]];
			}
			if ([dict objectForKey:@"button list"]) {
				[self setButtonsProperty:[dict objectForKey:@"button list"]];
			}
            if ([dict objectForKey:@"background"]) {
                [self setBackgroundProperty:[dict objectForKey:@"background"]];
            }
		}
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state.
}


- (void)dealloc {
    [super dealloc];
}

#pragma mark 扩展的字典引用方法
-(void) setBackgroundProperty:(NSDictionary*)bdict
{
    UIColor *bgc = [XRmenuPublic generalColor:[bdict objectForKey:@"background color"]];
    [self.contentView setBackgroundColor:bgc];
}
-(void) setButtonsProperty:(NSArray*)bts
{
	UIButton *bt;
	CGRect content = self.bounds;
	NSDictionary *ldict;
	for (NSDictionary *item in bts) {
		bt = nil;
		CGRect btframe = CGRectOffset(CGRectFromString([item objectForKey:@"button rect"]), content.origin.x, content.origin.y);
		ldict = nil;
		if ([item objectForKey:@"button lable"]) {
			ldict = [item objectForKey:@"button lable"];
		}
		
		bt = [XRmenuPublic generalButton:[item objectForKey:@"button style"] ButtonLable:[ldict objectForKey:@"state lable"] ButtonFrame:btframe];
		[bt setTag:(kTagOfStartBT + dictInt(item, @"button id"))];
		[bt addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchDown];
		
		if ([ldict objectForKey:@"font style"]) {
			[[bt titleLabel] setFont:[XRmenuPublic generalFont:[ldict objectForKey:@"font style"]]];
		}else {
			[[bt titleLabel] setFont:[XRmenuPublic generalFont:@"default font"]];
		}
		[self.contentView addSubview:bt];
	}
}

-(void) setImageViewsProperty:(NSArray *)imvs
{
	UIImageView *imgV;
	CGRect content = self.bounds;
	for (NSDictionary *item in imvs) {
		imgV = nil;
		CGRect imgVframe = CGRectOffset(CGRectFromString([item objectForKey:@"imageview rect"]), content.origin.x, content.origin.y);
		imgV=[XRmenuPublic generalImageView:imgVframe];
		[imgV setTag:(kTagOfStartImgV + dictInt(item, @"imageview id"))]; 
		
		if ([item objectForKey:@"imageview url"]) {
			NSDictionary *tr = [APPSESSION.translate objectForKey:[item objectForKey:@"imageview url"]];
			NSString *imgname;
			if (tr) {
				imgname = [XRmenuPublic getGeneralSkinImg:[tr objectForKey:APPSESSION.language]];
			}else {
				imgname = [XRmenuPublic getGeneralSkinImg:[item objectForKey:@"imageview url"]];
			}
			
			[imgV setImage:[UIImage imageWithContentsOfFile:imgname]];			
		}
		
		
		[self.contentView addSubview:imgV];
	}
	
}

-(void) setLabelsProperty:(NSArray*)lbs
{
	UILabel *lb;
	CGRect content = self.bounds;
	for (NSDictionary *item in lbs) {
		lb = nil;
		CGRect lbframe = CGRectOffset(CGRectFromString([item objectForKey:@"label rect"]), content.origin.x, content.origin.y);
		
		lb=[XRmenuPublic generalLabel:[item objectForKey:@"label style"] LabelText:[item objectForKey:@"text"] LabelFrame:lbframe];
		[lb setTag:(kTagOfStartLabel + dictInt(item, @"label id"))];
		[self.contentView addSubview:lb];
	}
}

#pragma mark 按钮处理
-(void)buttonClicked:(id)sender
{
	DebugLog(@"CellBase Button,请重载");
}
@end
